<?php

namespace app\store\model\appointment;

use app\common\model\BaseModel;


/**
 * 预约类型模型
 * Class GradeLog
 * @package app\store\model\user
 */
class Type extends BaseModel
{
    protected $name = 'appointment_type';

    /**
     * 预约类型
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList()
    {
        $where = [
            'status' => 1,
            'is_delete' => 0,
        ];
        if (isset($wxapp_id) && !empty($wxapp_id)) {
            $where['wxapp_id'] = self::$wxapp_id;
        }
        // 获取列表数据
        return $this->where($where)
            ->field('type_id,name')->select();
    }

    /*
     * 获取当前订单预约类型
     */
    public function getListType($type_id)
    {
        // 获取列表数据
        return $this->where('status', 1)
            ->where('is_delete', 0)
            ->where('type_id', $type_id)
            ->where('wxapp_id', self::$wxapp_id)
            ->field('type_id,name')->find();
    }

    /**
     * 预约类型
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getListByPage()
    {
        // 获取列表数据
        return $this
            ->where('is_delete', 0)
            ->paginate(15, false, [
                'query' => \request()->request()
            ])->each(function ($item, $key) {
                $users = Db("user_address")->where('user_id', 'in', explode(',', $item['service_id']))->column('name');
                $item['service_name'] = isset($users) ? implode(',', $users) : '';
            });
    }

    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
        log_print($data);

        if (!$this->validateForm($data)) {
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return false|int
     */
    public function edit($data)
    {
        if (!$this->validateForm($data, 'edit')) {
            return false;
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 类型详情
     * @param $grade_id
     * @param array $with
     * @return static|null
     * @throws \think\exception\DbException
     */
    public static function detail($type_id, $with = [])
    {
        return static::get($type_id, $with);
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     */
    private function validateForm($data, $scene = 'add')
    {
        if ($scene === 'add') {
            $exist = Db("appointment_type")->where('name', $data['name'])
                ->where('is_delete', 0)
                ->where('wxapp_id', self::$wxapp_id)
                ->value('type_id');
            if ($exist) {
                $this->error = '名称已存在';
                return false;
            }

        } elseif ($scene === 'edit') {
            $exist = Db("appointment_type")
                ->where('is_delete', 0)
                ->where('wxapp_id', self::$wxapp_id)
                ->where('type_id', 'neq', $this['type_id'])
                ->where('name', $data['name'])->value('type_id');
            if ($exist) {
                $this->error = '名称已存在';
                return false;
            }

        }

        if ($data['count'] < 1) {
            $this->error = '服务人员个数不能为0';
            return false;
        }
        return true;
    }
}